﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectBordspil.Models;
using ProjectBordspil.Sevices;
using System.Web.Security;


namespace ProjectBordspil.Controllers
{
    [Authorize]
    public class GameController : Controller
    {
        private IAppRepository _repository = null;

        /// <summary>
        /// constructor for the game controller. Sets the private variable _repository to a new
        /// instance of AppRepository
        /// </summary>
        public GameController()
        {
            _repository = new AppRepository();
        }

        /// <summary>
        /// Sets the _repository variable to rep. Used for unit testing.
        /// </summary>
        public GameController(IAppRepository rep)
        {
            _repository = rep;
        }
        
        /// <summary>
        /// Find an instance of a Memory game with some id
        /// </summary>
        public ActionResult ShowMemGame(int id)
        {

            MemServices mem = new MemServices();

            var oldGame = mem.showGameInstace(id);

            if (oldGame != null)
            {
                return View("MemGameView", oldGame);
            }

            return View("NotFound");
        }

 
        /// <summary>
        /// Creates a new instance of MemoryGame. The function then redirects operation to 
        /// the ShowMemGame function.
        /// </summary>
        [HttpPost]
        public ActionResult MemGameView(FormCollection fc)
        {

            MemServices mem = new MemServices();

            MemViewModel aSingleGame = new MemViewModel();

            aSingleGame.theGame = mem.CreateNewGame(fc["gameName"]);

            aSingleGame.theImages = mem.SetUpGame(aSingleGame.theGame.id);

            return RedirectToAction("ShowMemGame", new { id = aSingleGame.theGame.id } );
        }

        /// <summary>
        /// Creates a new instance of Guillotine game. The function then redirects operation to
        /// the ShowGuiGame function.
        /// </summary>
        [HttpPost]
        public ActionResult GuilGameView(string gameName)
        {

            GuiServices gui = new GuiServices();

            GuiViewModelModel aSingleGame = new GuiViewModelModel();

            aSingleGame.theGame = gui.CreateNewGuillotineGame(gameName);

            aSingleGame.Actions = gui.SetUpActions(aSingleGame.theGame.id);

            aSingleGame.Nobels = gui.SetUpNobels(aSingleGame.theGame.id);

            return RedirectToAction("ShowGuiGame", new { id = aSingleGame.theGame.id });
        }

        /// <summary>
        /// Find an instance of a Guillotine game with some id
        /// </summary>
        public ActionResult ShowGuiGame(int id)
        {

            GuiServices gui = new GuiServices();

            var oldGame = gui.showGameInstace(id);

            if (oldGame != null)
            {
                return View("GuilGameView", oldGame);
            }

            return View("NotFound");
        }

    }
}
